/*

 Copyright (c) 2008-2009 Fabrizio Pistonesi

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 */
#include "node.h"

Node::Node(IrrlichtDevice *device, s32 id, ENODE_TYPE type)
  {
    //ctor
    Device = device;
    Id = id;
    Type = type;
    //Root = Device->getSceneManager()->addEmptySceneNode(0, id);
    Root = Device->getSceneManager()->addCubeSceneNode(0,0,id);
    //GUIRoot = Device->getGUIEnvironment()->addGUIElement("GUIRoot");
    dimension2d<u32> size = Device->getVideoDriver()->getScreenSize();
    GUIRoot = Device->getGUIEnvironment()->addStaticText(L" ",rect<s32>(0,0,size.Width,size.Height));
    Pointer = Device->getSceneManager()->getActiveCamera();

    onEnter();
  }

//dtor
Node::~Node()
  {
    for (unsigned int i = 0; i < Sounds.size(); i++)
      {
	  Sounds.at(i)->~Sound();             //a.viarengo remove all sound
      }
    Root->remove();
    GUIRoot->remove();

    onLeave();
  }

void Node::onEnter()
  {

  }

void Node::onLeave()
  {

  }

void Node::setId(s32 newId)
  {
    Id = newId;
  }

s32 Node::getId()
  {
    return Id;
  }

ENODE_TYPE Node::getType()
  {
    return Type;
  }

void Node::setDefaultCursor(ECURSOR_ICON cursor)
  {
    DefaultCursor = cursor;
  }

void Node::setMouseMode(EMOUSE_MODE mouseMode)
  {
    MouseMode = mouseMode;
  }

//SKYBOX
void Node::addPatch(stringc image, bool visible, f32 x, f32 y, f32 w, f32 h,int face, SColor color)
  {
    printf("patch: %i\n",Patches.size());  //a.viarengo
    if (!Device->getFileSystem()->existFile(image.c_str()))
      {
        Device->getLogger()->log("Could not find the image: ", image.c_str());
        image = "missing.png";
      }
    Patches.push_back(new Patch(Device, image, visible, x, y, w, h, face, color) );
    Patches.back()->PatchRotation->setParent(Root);
  }

//SKYDOME
void Node::addPatch(stringc image, bool visible, f32 azimuth, f32 elevation,
    f32 w, f32 h, SColor color)
  {
    if (!Device->getFileSystem()->existFile(image.c_str()))
      {
        Device->getLogger()->log("Could not find the image: ", image.c_str());
        image = "missing.png";
      }
    Patches.push_back(new Patch(Device, image, visible, azimuth, elevation, h, w, color));  //a.viarengo
    Patches.back()->PatchRotation->setParent(Root);  //a.viarengo
  }

//PANEL
void Node::addPatch(stringc image, s32 x, s32 y, s32 w, s32 h, SColor color, bool visible)
  {
    if (!Device->getFileSystem()->existFile(image.c_str()))
      {
        Device->getLogger()->log("Could not find the image: ", image.c_str());
        image = "missing.png";
      }
    Patches.push_back(new Patch(Device, PanelGUI, visible, image, x, y, h, w, color));  //a.viarengo
  }

Patch *Node::getPatch(int index)
  {
    return Patches[index];
  }

void Node::addSound(stringc source, bool autostart, bool repeat,
    float volume, float pan, float pitch, f32 az, f32 el)
  {
    printf("sound: %i\n",Sounds.size());
    Sounds.push_back(new Sound(AudioDev, Pointer, source, autostart, repeat, volume, pan, pitch, az, el));  //a.viarengo
  }

Sound *Node::getSound(int index)
  {
    return Sounds[index];
  }

void Node::addSensor(f32 width, f32 height, f32 az, f32 el,s32 target,
int effect, float factor, int duration, int cursor, std::string message, bool enable)
{
  printf("sensor: %i\n",Sensors.size());
  Sensors.push_back(new Sensor(Device,width,height,az,el));
  Sensors.back()->setAction(target,effect,factor,duration,cursor,message,enable);
  Sensors.back()->SensorRotation->setParent(Root);
}

void Node::addSensor(s32 width, s32 height, s32 x, s32 y, s32 target,
int effect, float factor, int duration, int cursor, std::string message, bool enable)
{
  printf("sensor: %i\n",Sensors.size());
  Sensors.push_back(new Sensor(Device,PanelGUI,width,height,x,y));
  Sensors.back()->setAction(target,effect,factor,duration,cursor,message,enable);
}

Sensor *Node::getSensor(int index)
{
  return Sensors[index];
}

void Node::serialize(IAttributes* attributes)
{
  attributes->addInt("cursor",DefaultCursor);
  switch(Type)
  {
    case ENT_CUBIC:
      {
        attributes->addString("type","Cubic");
        attributes->addString("front",Sides[4].c_str());
        attributes->addString("right",Sides[5].c_str());
        attributes->addString("back",Sides[3].c_str());
        attributes->addString("left",Sides[1].c_str());
        attributes->addString("up",Sides[0].c_str());
        attributes->addString("down",Sides[2].c_str());
        attributes->addVector3d("rotation",Rotation);
      }
    case ENT_SPHERICAL:
      {
        attributes->addString("type","Spherical");
        attributes->addString("equirect",Equirect.c_str());
        attributes->addVector3d("rotation",Rotation);
      }
    case ENT_SLIDE:
      {
        attributes->addString("type","Slide");
        attributes->addString("background",Background.c_str());
      }
    case ENT_PANEL:
      {
        attributes->addString("type","Panel");
        attributes->addString("background", Background.c_str());
        attributes->addPosition2d("position", Position);
        attributes->addColor("color", Color);
      }
  }
}
